package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.cookbook.dao.CookbookCollectDAO;
import com.wanmait.cookbook.dao.CookbookDAO;
import com.wanmait.cookbook.dao.UserinfoDAO;
import com.wanmait.cookbook.dao.WorksCollectDAO;
import com.wanmait.cookbook.dao.WorksDAO;
import com.wanmait.cookbook.vo.Cookbook;
import com.wanmait.cookbook.vo.Pager;
import com.wanmait.cookbook.vo.Userinfo;
import com.wanmait.cookbook.vo.Works;

/**
 * Servlet implementation class FindBoxServlet
 */
@WebServlet("/FindBoxServlet")
public class FindBoxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindBoxServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String action =request.getParameter("action");
		
		if("works".equals(action)) {//点击作品按钮添砖的页面
			this.findboxWorks(request, response);
		}else if("userinfo".equals(action)) {//点击用户界面跳转的页面
			this.findboxUserinfo(request, response);
		}else if("find".equals(action)) {//搜索框搜索和点击菜谱跳转的页面
			this.findboxInitCookbook(request, response);
		}else if("countCookbook".equals(action)){//点击菜谱界面的分页数字跳转界面
			this.numbCookbook(request, response);
		}else if("countWorks".equals(action)) {//作品界面的数字跳转
			this.numbWorks(request, response);
		}else if("countUserinfo".equals(action)) {//用户界面的数字跳转
			this.numbUserinfo(request, response);
		}else if("cookbook".equals(action)){
			this.findboxCookbook(request, response);
		}
		
		else {
			//this.findboxInitCookbook(request, response);
		}
		
		
	}
	//ajax点击数字查找做作品
	public void numbWorks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("name");
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		
		pager.setPageSize(2);
		int rows=WorksDAO.getWorksDAO().getBlurryRows(name);
		pager.setPageCount(rows);
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		pager.setPageNo(Integer.parseInt(pageNo));
		List<Works> workss=WorksDAO.getWorksDAO().burryName(name,pager);
		List<Integer> worksCount=new ArrayList<Integer>();
		for (Works works : workss) {
			int count=WorksCollectDAO.getWorksCollectDAO().worksCollectCount(works);
			worksCount.add(count);
		}
		request.setAttribute("pager", pager);
		request.setAttribute("name", name);
		request.setAttribute("worksCount",worksCount );
		request.setAttribute("workss",workss);
		request.getRequestDispatcher("/inc/ajaxInc/InquireWorksInc.jsp").forward(request, response);
	}
	
	
	
	//点击框查找作品
	public void findboxWorks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("name");
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(2);
		int rows=WorksDAO.getWorksDAO().getBlurryRows(name);
		pager.setPageCount(rows);
		List<Works> workss=WorksDAO.getWorksDAO().burryName(name,pager);
		List<Integer> worksCount=new ArrayList<Integer>();
		for (Works works : workss) {
			int count=WorksCollectDAO.getWorksCollectDAO().worksCollectCount(works);
			worksCount.add(count);
		}
		//System.out.println(name+"works");
		request.setAttribute("pager", pager);
		request.setAttribute("name", name);
		request.setAttribute("worksCount",worksCount );
		request.setAttribute("workss",workss);
		request.getRequestDispatcher("/InquireWorksShowList.jsp").forward(request, response);
	}
	//ajax点击数字查找作者
	public void numbUserinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("name");
		String pageNo=request.getParameter("pageNo");
		Pager pager =new Pager();
		
		int rows=UserinfoDAO.getUserinfoDAO().getBlurryRows(name);
		pager.setPageCount(rows);
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		pager.setPageNo(Integer.parseInt(pageNo));
		pager.setPageSize(2);
		List<Userinfo> userinfos=UserinfoDAO.getUserinfoDAO().blurryName(name,pager);
		
		request.setAttribute("pager", pager);
		request.setAttribute("name", name);
		request.setAttribute("userinfos", userinfos);
		request.getRequestDispatcher("/inc/ajaxInc/InquireUserinfoInc.jsp").forward(request, response);
	}
	//ajax查询框查找作者
	public void findboxUserinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("name");
		Pager pager =new Pager();
		pager.setPageNo(1);
		pager.setPageSize(2);
		int rows=UserinfoDAO.getUserinfoDAO().getBlurryRows(name);
		pager.setPageCount(rows);
		List<Userinfo> userinfos=UserinfoDAO.getUserinfoDAO().blurryName(name,pager);
		
		request.setAttribute("pager", pager);
		request.setAttribute("name", name);
		request.setAttribute("userinfos", userinfos);
		request.getRequestDispatcher("/InquireUserinfoShowList.jsp").forward(request, response);
	}
	
	//ajax点击菜谱数字后跳转
	public void numbCookbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("name");
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(2);
		int rows=CookbookDAO.getCookbookDAO().getBlurryRows(name);
		pager.setPageCount(rows);
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		pager.setPageNo(Integer.parseInt(pageNo));
	
		
		
		List<Cookbook> cookbooks=CookbookDAO.getCookbookDAO().blurryName(name,pager);
		List<Integer> cookbookCounts=new ArrayList<Integer>();
		for (Cookbook cookbook : cookbooks) {
			int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
			cookbookCounts.add(count);
		}
	
		request.setAttribute("pager", pager);
		request.setAttribute("name", name);
		request.setAttribute("cookbookCounts", cookbookCounts);
		request.setAttribute("cookbooks", cookbooks);
		request.getRequestDispatcher("/inc/ajaxInc/InquireCookbookInc.jsp").forward(request, response);
	
	}
	//点击菜谱时执行
	public void findboxCookbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//要查询的模糊词
		String name=request.getParameter("name");
		//数字。页数
		Pager pager=new Pager();
		pager.setPageSize(2);
		pager.setPageNo(1);
		int rows=CookbookDAO.getCookbookDAO().getBlurryRows(name);
		pager.setPageCount(rows);
		//展示菜谱
		
		List<Cookbook> cookbooks=CookbookDAO.getCookbookDAO().blurryName(name,pager);
		List<Integer> cookbookCounts=new ArrayList<Integer>();
		for (Cookbook cookbook : cookbooks) {
			int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
			cookbookCounts.add(count);
		}
		
		
		request.setAttribute("pager", pager);
		request.setAttribute("name", name);
		//转发菜谱
		request.setAttribute("cookbookCounts", cookbookCounts);
		request.setAttribute("cookbooks", cookbooks);
		//转发到界面
		request.getRequestDispatcher("/InquireCookbookShowList.jsp").forward(request, response);
	
	}
	//查询框查找菜谱(头页面点击查询后跳转)
		public void findboxInitCookbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
			//要查询的模糊词
			String name=request.getParameter("keywords");
			//数字。页数
			Pager pager=new Pager();
			pager.setPageSize(2);
			pager.setPageNo(1);
			int rows=CookbookDAO.getCookbookDAO().getBlurryRows(name);
			pager.setPageCount(rows);
			//展示菜谱
			
			List<Cookbook> cookbooks=CookbookDAO.getCookbookDAO().blurryName(name,pager);
			List<Integer> cookbookCounts=new ArrayList<Integer>();
			for (Cookbook cookbook : cookbooks) {
				int count=CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
				cookbookCounts.add(count);
			}
			
			
			request.setAttribute("pager", pager);
			request.setAttribute("name", name);
			//转发菜谱
			request.setAttribute("cookbookCounts", cookbookCounts);
			request.setAttribute("cookbooks", cookbooks);
			//转发到界面
			request.getRequestDispatcher("/InquireCookbookShowList.jsp").forward(request, response);
		
		}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
